﻿using LuDragon.Data.Entities;
using LuDragon.IService.System;
using LuDragon.Web.Areas.Admin.Filters;
using System;
using System.Linq;
using System.Web.Mvc;

namespace LuDragon.Web.Areas.Admin.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [CheckLogin]
    public class UserController : BaseController
    {
        private IUserService userServer;
        private IRoleService roleService;
        public UserController(IUserService _userService,IRoleService _roleService)
        {
            userServer = _userService;
            roleService = _roleService;
        }
        #region 视图
        /// <summary>
        /// 忘记密码
        /// </summary>
        /// <returns></returns>
        public ActionResult ForgetPassword()
        {
            return View();
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdPassword()
        {
            return View();
        }
        /// <summary>
        /// 用户列表
        /// </summary>
        /// <returns></returns>
        public ActionResult UserList()
        {
            var userList = userServer.GetAll().ToList();  //获取所有用户列表
            var roleList = roleService.GetAll().ToList();  //获取所有角色列表
            ViewBag.UserList = userList;
            ViewBag.RoleList = roleList;
            return View();
        }
        /// <summary>
        /// 用户编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult UserEdit(int id = 0)
        {
            var user = userServer.GetModel(a=>a.UserId==id);
            ViewBag.User = user;
            var roleList = roleService.GetAll().ToList();  //获取所有角色列表
            ViewBag.RoleList = roleList;
            return View();
        }
        #endregion
        #region 数据
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Del(int id)
        {
            return Success(userServer.Del(id));
        }
        /// <summary>
        /// 禁用用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Disabled(int userId) {
            userServer.Disabled(userId);
            WriteLog("用户",Common.Enums.LogMode.NotEnabled,userServer.GetModel(s=>s.UserId==userId).UserName);
            return Success();
        }
        /// <summary>
        /// 启用用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Enabled(int userId)
        {
            userServer.Enabled(userId);
            WriteLog("用户", Common.Enums.LogMode.Enabled, userServer.GetModel(s => s.UserId == userId).UserName);
            return Success();
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(Sys_User user) {
            if (user.RegisterTime==null)
            {
                user.RegisterTime = DateTime.Now;
            }
            userServer.Save(user);
            if (user.UserId > 0)
            {
                WriteLog("用户", Common.Enums.LogMode.Update, user.UserName);
            }
            else {
                WriteLog("用户",Common.Enums.LogMode.Add,user.UserName);
            }
            return Success();
        }
        #endregion

    }
}